package com.lipuwater.agent.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blekit.BluetoothClient;
import com.ismartlib.ui.view.tablayout.TabLayoutSmk;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseActivity;
import com.lipuwater.agent.getui.GeTuiPushEvent;
import com.lipuwater.agent.service.AppVersionUpdate;
import com.lipuwater.agent.ui.event.KeyDownEvent;
import com.lipuwater.agent.ui.fragment.FragmentHome;
import com.lipuwater.agent.ui.fragment.FragmentMy;
import com.lipuwater.agent.ui.fragment.FragmentTraining;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


public class MainActivity extends BaseActivity implements TabLayoutSmk.CallbackPosition {

    @Bind(R.id.tabLayout)
    TabLayoutSmk tabLayout;
    @Bind(R.id.text_tab)
    TextView textTab;
    @Bind(R.id.layout_right)
    public RelativeLayout layoutRight;
    @Bind(R.id.layout_left_back)
    public LinearLayout layoutLeftBack;
    public boolean mVisibleShare = false;
    public boolean mVisibleBack = false;

    //标签切换
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private List<Integer> mIconUnselectIds = new ArrayList<>();
    private List<Integer> mIconSelectIds = new ArrayList<>();
    public static BluetoothClient mBleClient;
    public int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setStatusBar(R.id.view_status_bar);
        setSwipeBackEnable(false);
        ButterKnife.bind(this);
        initTab();
        mBleClient = new BluetoothClient(this);
        new AppVersionUpdate(MainActivity.this, token);

        EventBus.getDefault().register(this);
//        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), GetTuiIntentService.class);
    }

    private void initTab() {
        fragments.add(new FragmentHome());
        //   fragments.add(new FragmentConsulting());
        fragments.add(new FragmentTraining());
        fragments.add(new FragmentMy());
        mTitles.add("主页");
        //mTitles.add("资讯");
        mTitles.add("培训");
        mTitles.add("我的");
        mIconUnselectIds.add(R.drawable.tab_home_default);
        // mIconUnselectIds.add(R.drawable.tab_consulting_default);
        mIconUnselectIds.add(R.drawable.tab_training_default);
        mIconUnselectIds.add(R.drawable.tab_my_default);
        mIconSelectIds.add(R.drawable.tab_home_down);
        //  mIconSelectIds.add(R.drawable.tab_consulting_down);
        mIconSelectIds.add(R.drawable.tab_training_down);
        mIconSelectIds.add(R.drawable.tab_my_down);

        tabLayout.init(fragments, mTitles, mIconUnselectIds, mIconSelectIds, getSupportFragmentManager(), this);
        tabLayout.setTextSelectColor(Color.parseColor("#228EFD"));
    }

    public void onEventMainThread(GeTuiPushEvent event) {
        Toast.makeText(this, event.getJson(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void positon(int position) {
        this.position = position;
        switch (position) {
            case 0:
                textTab.setText("主页");
                layoutRight.setVisibility(View.GONE);
                layoutLeftBack.setVisibility(View.GONE);
                break;
            case 1:
                textTab.setText("培训");
                if (mVisibleShare) {
                    layoutRight.setVisibility(View.VISIBLE);
                }
                if (mVisibleBack) {
                    layoutLeftBack.setVisibility(View.VISIBLE);
                }
                break;
            case 2:
                textTab.setText("我的");
                layoutRight.setVisibility(View.GONE);
                layoutLeftBack.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            EventBus.getDefault().post(new KeyDownEvent(keyCode, event, position));
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }


    public long mExitTime;

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBleClient != null) {
            mBleClient.closeBluetooth();
            mBleClient = null;
        }
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

}
